package com.zsj.service_user.controller;


import com.zsj.common_util.R;
import com.zsj.service_user.entity.AppOrder;
import com.zsj.service_user.service.AppOrderService;
import com.zsj.service_user.util.TokenMd5;
import jdk.nashorn.internal.parser.Token;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zsj
 * @since 2022-05-03
 */
@RestController
@RequestMapping("/serviceUser/order")
@CrossOrigin
@Slf4j
public class AppOrderController {

    @Autowired
    AppOrderService orderService;


    /**
     * 添加订单的方法
     * @param token
     * @param order
     * @return
     */
    @PostMapping("/addOrder")
    public R getAllByName(@RequestParam("token") String token, @RequestBody AppOrder order) {
        if (!TokenMd5.getMD5(order.getOrderBelongtoUser()).equals(token)) {
            return R.error().message("身份验证失败");
        } else {
            if (orderService.saveOrder(order)) {
                return R.ok().message("添加订单成功");
            } else return R.error().message("添加订单失败");
        }

    }

    /**
     * 获取订单根据账号名
     * @param name
     * @return
     */
    @GetMapping("/getOrdersByNameAndStatus")
    public R getAllByName(@RequestParam("name") String name,@RequestParam("status") String status) {
        List<AppOrder> all = orderService.getAllByName(name,status);
        if (all.isEmpty()) {
            log.error("getAllByName查询失败，数据为空");
            return R.ok().message("查询失败，数据为空").data("list", all);
        } else {
            return R.ok().data("list", all);
        }
    }


}

